package com.fanyue.modules.junit;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.arcsoft.face.*;
import com.arcsoft.face.enums.*;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import com.arcsoft.face.toolkit.ImageInfoEx;

import java.io.File;
import java.util.*;

public class FaceEngineTest2 {

    private static Map<String, FaceEngine> faceEngineCenter = new HashMap<>();

    public static void main(String[] args) {
        init();
        FaceFeature faceFeature = register();
        compare(faceFeature);
    }

    public static void compare(FaceFeature faceFeature) {
        byte[] featureData = faceFeature.getFeatureData();
        Console.log(featureData.length);

        String encode = Base64.encode(featureData);
        byte[] bytes = Base64.decode(encode);
        Console.log(bytes.length);
        boolean  equals = Arrays.equals(featureData, bytes);
        Console.log(equals);

        String str = Convert.toStr(featureData);
        str = str.replace("[", "").replace("]", "");
        Byte[] byteArray = Convert.toByteArray(str);
        byte[] byteArray2 = new byte[byteArray.length];
        for (int i = 0; i < byteArray.length; i++) {
            byteArray2[i] = byteArray[i]; // 自动拆箱
        }
        Console.log(byteArray2.length);
    }

    public static FaceFeature register() {

        FaceEngine faceEngine = faceEngineCenter.get("wzb");
        int errorCode = 0;

        // 人脸检测
        ImageInfo imageInfo = ImageFactory.getRGBData(new File("D:\\我的文件\\1\\22.jpg"));
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        errorCode = faceEngine.detectFaces(imageInfo, faceInfoList);
        System.out.println("人脸检测errorCode:" + errorCode);
        System.out.println("检测到人脸数:" + faceInfoList.size());

        ImageQuality imageQuality = new ImageQuality();
        errorCode = faceEngine.imageQualityDetect(imageInfo, faceInfoList.get(0), 0, imageQuality);
        System.out.println("图像质量检测errorCode:" + errorCode);
        System.out.println("图像质量分数:" + imageQuality.getFaceQuality());

        // 特征提取
        FaceFeature faceFeature = new FaceFeature();
        errorCode = faceEngine.extractFaceFeature(imageInfo, faceInfoList.get(0), ExtractType.REGISTER, 0, faceFeature);
        System.out.println("特征提取errorCode:" + errorCode);

        // 注册人脸信息1
        FaceFeatureInfo faceFeatureInfo = new FaceFeatureInfo();
        faceFeatureInfo.setSearchId(5);
        faceFeatureInfo.setFaceTag("FeatureData1");
        faceFeatureInfo.setFeatureData(faceFeature.getFeatureData());
        errorCode = faceEngine.registerFaceFeature(faceFeatureInfo);

        // 获取注册人脸个数
        FaceSearchCount faceSearchCount = new FaceSearchCount();
        errorCode = faceEngine.getFaceCount(faceSearchCount);
        System.out.println("注册人脸个数:" + faceSearchCount.getCount());

        // 引擎卸载
        // errorCode = faceEngine.unInit();
        return faceFeature;
    }

    public static void init() {
        // 激活码，从官网获取
        String appId = "kjRRG6W1ywDoe3booT9t6thK6BGqWmvJEq4xJ3xaTEH";
        String sdkKey = "9vB3XSDhhVs2cadz3AjjcPSnGQSybfuFMemiVcZK5jU";
        String activeKey = "86L1-11RX-X13J-7X29";

        System.err.println("注意，如果返回的errorCode不为0，可查看com.arcsoft.face.enums.ErrorInfo类获取相应的错误信息");

        // 人脸识别引擎库存放路径
        FaceEngine faceEngine = new FaceEngine("D:\\Env\\Arcsoft\\lib");
        // 激活引擎
        int errorCode = faceEngine.activeOnline(appId, sdkKey, activeKey);
        System.out.println("引擎激活errorCode:" + errorCode);

        ActiveDeviceInfo activeDeviceInfo = new ActiveDeviceInfo();
        // 采集设备信息（可离线）
        errorCode = faceEngine.getActiveDeviceInfo(activeDeviceInfo);
        System.out.println("采集设备信息errorCode:" + errorCode);
        System.out.println("设备信息:" + activeDeviceInfo.getDeviceInfo());

//        faceEngine.activeOffline("d:\\ArcFacePro64.dat.offline");

        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
        errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
        System.out.println("获取激活文件errorCode:" + errorCode);
        System.out.println("激活文件信息:" + activeFileInfo.toString());

        // 引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setDetectFaceMaxNum(10);
        // 功能配置
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportAge(true);
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        functionConfiguration.setSupportGender(true);
        functionConfiguration.setSupportLiveness(true);
        functionConfiguration.setSupportIRLiveness(true);
        functionConfiguration.setSupportImageQuality(true);
        functionConfiguration.setSupportMaskDetect(true);
        // functionConfiguration.setSupportUpdateFaceData(true);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);

        // 初始化引擎
        errorCode = faceEngine.init(engineConfiguration);
        System.out.println("初始化引擎errorCode:" + errorCode);
        VersionInfo version = faceEngine.getVersion();
        System.out.println(version);

        faceEngineCenter.put("wzb", faceEngine);
    }
}